home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / ABox.v1.8 / CPlus Files / ABOtherTopic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-23  |  14.5 KB  |  628 lines  |  [TEXT/MMCC]

  1. /*    
  2.     Copyright © 1991-1995 by TopSoft Inc.  All rights reserved.
  3.  
  4.     You may distribute this file under the terms of the TopSoft
  5.     Artistic License, accompanying this package.
  6.     
  7.     This file was developed by George (ty) Tempel in connection with TopSoft, Inc..
  8.     See the Modification History for more details.
  9.  
  10. Product
  11.     About Box
  12.  
  13. FILE
  14.     ABOtherTopic.c
  15.  
  16. NAME
  17.     ABOtherTopic.c, part of the ABox project source code,
  18.     responsible for handling the ABOtherTopic class stuff.
  19.  
  20. DESCRIPTION
  21.     This file contains defines for the about box modules.
  22.     
  23. DEVELOPED BY
  24.     George (ty) Tempel                netromancr@aol.com
  25.     All code in this file, and its associated header file was
  26.     Created by George (ty) Tempel in connection with the TopSoft, Inc.
  27.     "FilterTop" application development, except where noted.
  28.  
  29. CARETAKER - George (ty) Tempel <netromancr@aol.com>
  30.      Please consult this person for any changes or suggestions to this file.
  31.  
  32. MODIFICATION HISTORY
  33.  
  34.     dd mmm yy    -    xxx    -    patchxx: description of patch
  35.     13 july 94    -    ty    -    Initial Version Created
  36.     20-july-94    -    ty    -    initial version released
  37.     28-july-94    -    ty    -    1.0.6 -- removed cursor flipping from Load
  38.     10-apr-95    -    ty    -    added support for TeachText/SimpleText 'ttro'
  39.                             documents (but only as text, no pict processing)
  40.     23-may-95    -    ty    -    changes for compatibility with the CodeWarrior CW6
  41.                             release and the associated Universal Headers from Apple:
  42.                             most methods that returned references now have "Ref" at
  43.                             the end of their methods names to prevent possible collisions
  44.                             with datatypes and classes of the same name (older versions
  45.                             of the compiler didn't have a problem with this).
  46. */
  47.  
  48. /*===========================================================================*/
  49.  
  50. /*======= Segmentation directives ========*/
  51.  
  52. #ifdef USE_MANUAL_SEGMENTATION
  53. #pragma segment ty
  54. #endif
  55.  
  56. /*============ Header files ==============*/
  57.     
  58. #include     "ABOtherTopic.h"
  59. #include    "ABoxDefs.h"
  60.  
  61.  
  62. /*=============== Globals ================*/
  63.  
  64. /*================ CODE ==================*/
  65.  
  66.  
  67. /*=============================== ABOtherTopic::ABOtherTopic ================================*/
  68. ABOtherTopic::ABOtherTopic(const FSSpec& inTopicFSSpec)
  69.     :    ABTopic(inTopicFSSpec)
  70. {
  71.     this->ResetSlideInfo();
  72.  
  73.     this->BlurbRef() = NULL;
  74.     this->FileTypeRef() = kABbadFileType;
  75.     this->TopicType() = ETopicType_OtherTopic;
  76. } // end ABOtherTopic
  77.  
  78.  
  79.  
  80.  
  81. /*=============================== ABOtherTopic::~ABOtherTopic ================================*/
  82. ABOtherTopic::~ABOtherTopic(void)
  83. {
  84.     if (this->HasBlurb())
  85.     {
  86.         delete this->BlurbRef();
  87.         this->BlurbRef() = NULL;
  88.     }
  89.     
  90. } // end ~ABOtherTopic
  91.  
  92.  
  93.  
  94.  
  95. /*=============================== ABOtherTopic::GetProperty ================================*/
  96. OSErr    ABOtherTopic::GetProperty(ABProperty prop, 
  97.                                 void *ptr, 
  98.                                 long *ptrSize)
  99. {
  100.     OSErr    error = noErr;
  101.     long    pSize;
  102.     
  103.     //    begin here...
  104.     
  105.     if (!ptr)
  106.         return kABPropertyNullStorage;
  107.     
  108.     switch (prop)
  109.     {
  110.         case    kABTopicIsFolder:
  111.                     *((Boolean *)ptr) = (this->FileTypeRef() == kABFolderFileType);
  112.                     pSize = kABTopicIsFolderSize;
  113.                     break;
  114.         case    kABTopicFileType:
  115.                     *((ABTopicFileType *)ptr) = this->FileTypeRef();
  116.                     pSize = kABTopicFileTypeSize;
  117.                     break;
  118.         default:
  119.                     error = kABOtherTopicSuperProperties::GetProperty(prop, ptr, ptrSize);
  120.                     break;
  121.     } // end switch block
  122.     
  123.     if (ptrSize && !error)
  124.         *ptrSize = pSize;
  125.     return error;
  126.     
  127. } // end GetProperty
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*=============================== ABOtherTopic::SetProperty ================================*/
  134. OSErr    ABOtherTopic::SetProperty(ABProperty prop, 
  135.                                 void *ptr, 
  136.                                 long ptrSize)
  137. {
  138.     OSErr            error = noErr;
  139.     
  140.     //    begin here...
  141.     
  142.     if (!ptr)
  143.         return kABPropertyNullStorage;
  144.     
  145.     switch (prop)
  146.     {
  147.         case    kABTopicIsFolder:
  148.                     error = kABPropertyReadOnly;
  149.                     break;
  150.  
  151.         default:
  152.                     error = kABOtherTopicSuperProperties::SetProperty(prop, ptr, ptrSize);
  153.                     break;
  154.     } // end switch block
  155.     
  156.     return error;
  157.     
  158. } // end SetProperty
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*=============================== ABOtherTopic::Resize ===============================*/
  167. //
  168. //    this function will rearrange and resize the picture and the
  169. //    text fields that appear on a slide.
  170. //
  171. //    This method will cause the current slide for the
  172. //    topic to resize by invoking that item's resize method.
  173. //
  174. //    is called by:
  175. //
  176. OSErr    ABOtherTopic::Resize (Rect const *field)
  177. {
  178.     OSErr    error = noErr;
  179.     Rect    box;
  180.     
  181.     //    begin here...
  182.     
  183.     error = ABTopic::Resize(field);
  184.     if (error == noErr)
  185.     {
  186.         box = *field;
  187.         if (this->HasBlurb())
  188.             error = this->BlurbRef()->SetProperty(kABObjectRect, &box, kABObjectRectSize);
  189.         else
  190.             error = paramErr;
  191.     } // endif else block
  192.     
  193.     return error;
  194. } // end of Resize
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /*=============================== ABOtherTopic::OpenTopic ================================*/
  201. OSErr    ABOtherTopic::OpenTopic(void)
  202. {
  203.     OSErr        error = noErr;
  204.  
  205.     Handle        dataHandle = NULL;
  206.     short        blurbResID;
  207.     short        tempRefNum;
  208.     long        dataCount;
  209.     const long    headerOffset = 512;
  210.     const long    thirtyTwoKLimit = 32767;
  211.     
  212.     //    begin here...
  213.     
  214.     //    now load the data and create a "resource" outta it...
  215.     //
  216.     ABUCursor::WatchCursor();
  217.     if (this->HasBlurb())
  218.     {
  219.         delete this->BlurbRef();
  220.         this->BlurbRef() = NULL;
  221.     }
  222.     
  223.     //    now...if the "fileType" is something like kABfolderFileType
  224.     //    then we're dealing with a folder, and we should just display
  225.     //    the standard folder message (which comes from a resource).
  226.     //    If, however, the fileType is something else we'd be interested
  227.     //    in, then we need to create some storage.
  228.     if (this->DoesntHaveFSSpecPointer() && this->IsNotFolder())
  229.     {
  230.         ABUCursor::ArrowCursor();
  231.         return resNotFound;
  232.     }
  233.         
  234.     switch (this->GetFileType())
  235.     {
  236.         case    kABFolderFileType:
  237.                 //    yup, it's a folder, so set resourceID to use
  238.                 //
  239.                 //    switch to the ABox resource file first...
  240.                 (void)this->UseTopicResFile();
  241.     
  242.                 blurbResID = kABfolderTextSlide;
  243.                 this->BlurbRef() = new ABText;
  244.                 break;
  245.         case    kABbadFileType:
  246.                 //    huh? this shouldn't happen...
  247.                 ABUCursor::ArrowCursor();
  248.                 return resNotFound;
  249.                 break;
  250.         case    kABPictResource:
  251.         case    kABTextResource:
  252.         case    kABTeachTextReadOnly:
  253.                 //    something else, so we need to create some storage
  254.                 
  255.                 //    open the file...
  256.                 error = ::FSpOpenDF (this->FSSpecPointerRef(), fsRdPerm, &tempRefNum);
  257.                 if (error)
  258.                     return error;
  259.                 
  260.                 //    find out how large
  261.                 error = ::GetEOF(tempRefNum, &dataCount);
  262.                 if (error)
  263.                 {
  264.                     ::FSClose (tempRefNum);
  265.                     ABUCursor::ArrowCursor();
  266.                     return error;
  267.                 } // end if
  268.                 
  269.                 //    create some storage...
  270.                 dataHandle = ::NewHandle (dataCount);
  271.                 error = ::MemError();
  272.                 if (error)
  273.                 {
  274.                     ::FSClose (tempRefNum);
  275.                     ABUCursor::ArrowCursor();
  276.                     return error;
  277.                 } // end if
  278.             
  279.                 //    read the file's data into memory
  280.  
  281.                 switch (this->GetFileType())
  282.                 {
  283.                     case    kABFolderFileType:
  284.                             //    this shouldn't get here...then again
  285.                             error = resNotFound;
  286.                             break;
  287.                     case    kABPictResource:
  288.                             //    read a PICT file:
  289.                             //    advance past the 512 byte header of a PICT file
  290.                             //
  291.                             this->BlurbRef() = new ABPict;
  292.                             error = ::SetFPos ( tempRefNum, fsFromStart, headerOffset );
  293.                             dataCount -= headerOffset;
  294.                             ::SetHandleSize (dataHandle, dataCount);
  295.                             break;
  296.                     case    kABTeachTextReadOnly:
  297.                             //    added support for the 'ttro' teach-/simple-text read
  298.                             //    only text document. We must do this because the file
  299.                             //    type gets mapped to a resource type
  300.                             this->FileTypeRef() = kABTextResource;
  301.                             
  302.                             //    fall-thru is intentional!
  303.                             
  304.                     case    kABTextResource:
  305.                             //    limit the stuff read to 32K!
  306.                             this->BlurbRef() = new ABText;
  307.                             if (dataCount > thirtyTwoKLimit)
  308.                             {
  309.                                 dataCount = thirtyTwoKLimit;
  310.                                 ::SetHandleSize (dataHandle, dataCount);
  311.                             } // end if block
  312.                             
  313.                             break;
  314.                     default:
  315.                             //    huh?
  316.                             error = resNotFound;
  317.                             break;
  318.                 } // end switch block
  319.                         
  320.                 if (error || this->DoesntHaveBlurb())
  321.                 {
  322.                     if (!error && this->DoesntHaveBlurb())
  323.                         error = ::MemError();
  324.                     ::FSClose (tempRefNum);
  325.                     ::DisposHandle(dataHandle);
  326.                     ABUCursor::ArrowCursor();
  327.                     return error;
  328.                 } // end if
  329.                 
  330.                 //    now read the data into the handle and close up the file
  331.                 ::HLock(dataHandle);
  332.                 error = ::FSRead ( tempRefNum, &dataCount, *dataHandle);
  333.                 ::HUnlock (dataHandle);
  334.                 (void)::FSClose (tempRefNum);
  335.                 if (error)
  336.                 {
  337.                     ::DisposHandle(dataHandle);
  338.                     ABUCursor::ArrowCursor();
  339.                     return error;
  340.                 } // end if
  341.             
  342.                 //    synthesize the resource so the ABResource object can use it properly
  343.                 //    <pretty sneaky, eh?>
  344.                 //
  345.                 //    switch to the resource file (hopefully, the application one)
  346.                 if (this->DoesntHaveFileRefNum())
  347.                     ::UseResFile(this->GetFileRefNum());
  348.             
  349.                 blurbResID = ::UniqueID(this->GetFileType());
  350.                 ::AddResource (dataHandle, 
  351.                             this->GetFileType(), 
  352.                             blurbResID,
  353.                             kABSyntheticResourceName);
  354.                 error = ::ResError();
  355.                 if (error)
  356.                 {
  357.                     ABUCursor::ArrowCursor();
  358.                     return error;
  359.                 } // end if block
  360.     
  361.                 //    now shove the resource into the ABResource object
  362.                 error = this->BlurbRef()->SetProperty (kABResourceHandle, &dataHandle, kABResourceHandleSize);
  363.                 break;
  364.         default:
  365.                 //    huh? we should never get here...
  366.                 error = resNotFound;
  367.                 break;
  368.     } // end switch block
  369.     
  370.     if (error == noErr)
  371.         if (this->HasBlurb())
  372.             //    set the resource ID for the ABResource object
  373.             error = this->BlurbRef()->SetProperty (kABResourceResID, &blurbResID, kABResourceResIDSize);
  374.         else
  375.             error = ::MemError();
  376.             
  377.  
  378.     ABUCursor::ArrowCursor();
  379.     
  380.     return error;
  381. } // end OpenTopic
  382.  
  383.  
  384.  
  385.  
  386.  
  387. /*=============================== ABOtherTopic::CloseTopic ================================*/
  388. OSErr    ABOtherTopic::CloseTopic(void)
  389. {
  390.     OSErr        error = noErr;
  391.     Handle        resHandle;
  392.     short        resAttributes;
  393.     
  394.     //    begin here...
  395.     ABUCursor::WatchCursor();
  396.  
  397.     if (this->HasBlurb())
  398.         error = this->BlurbRef()->GetProperty (kABResourceHandle, &resHandle, NULL);
  399.         
  400.     if ((error == noErr) && resHandle && this->HasBlurb())
  401.     {
  402.         if (this->IsNotFolder())
  403.         {
  404.             short        oldResFile = this->UseTopicResFile();
  405.     
  406.             resAttributes = ::GetResAttrs(resHandle);
  407.             ::RmveResource (resHandle);
  408.             error = ::ResError();
  409.             if (resAttributes & resChanged)
  410.             {
  411.                 resAttributes ^= resChanged;
  412.                 ::SetResAttrs (resHandle, resAttributes);
  413.                 error = ::ResError();
  414.             } // end if block
  415.             
  416.             ::RmveResource (resHandle);
  417.             ::UpdateResFile (this->GetFileRefNum());
  418.             error = ::ResError();
  419.             ::DisposHandle ((Handle)resHandle);
  420.             resHandle = NULL;
  421.             error = this->BlurbRef()->SetProperty (kABResourceHandle, &resHandle, kABResourceHandleSize);
  422.             
  423.             ::UseResFile(oldResFile);
  424.             
  425.         } // end if block
  426.         
  427.         delete this->BlurbRef();
  428.         this->BlurbRef() = NULL;
  429.     } // end if block
  430.  
  431.     ABUCursor::ArrowCursor();
  432.     return error;
  433. } // end CloseTopic
  434.  
  435.  
  436.  
  437.  
  438.  
  439. /*=============================== ABOtherTopic::DoLoad ===============================*/
  440. //
  441. OSErr    ABOtherTopic::DoLoad (void)
  442. {
  443.     return noErr;
  444.     
  445. }    //    end of function DoLoad
  446.  
  447.  
  448.  
  449.  
  450.  
  451. /*=============================== ABOtherTopic::DoCheckSlides ===============================*/
  452. //
  453. //    This function checks a file's type and will return a zero value
  454. //    if it is not acceptable, or a non-zero valuee(1) to indicate
  455. //    that it is acceptable.
  456. //
  457. //    is called by:
  458. //
  459. short    ABOtherTopic::DoCheckSlides (void)
  460. {
  461.     const short        badCount = 0;
  462.     short            count = badCount;
  463.     FInfo            fileInfo;
  464.     OSErr            error = noErr;
  465.     
  466.     const Boolean    resolvedAliasChains = true;
  467.     Boolean            targetIsFolder = false;
  468.     Boolean            wasAliased = false;
  469.     
  470.     //    begin here...
  471.     
  472.     
  473.     if (this->DoesntHaveFSSpecPointer())
  474.         return badCount;
  475.     
  476.     //    i can't assume that the alias has been resolved, nor do
  477.     //    i want to at this point. What i'm really concerned with is
  478.     //    whether the FSSpecPtr describes a file (which will be useful),
  479.     //    or a folder (which will also be useful). Since FSpGetFInfo()
  480.     //    can't tell me if the file is/isn't a folder, i'll use
  481.     //    ResolveAliasFile and only pay attention to the targetIsFolder
  482.     //    return value.
  483.     
  484.     error = ::ResolveAliasFile (this->FSSpecPointerRef(), 
  485.                                 resolvedAliasChains, 
  486.                                 &targetIsFolder, 
  487.                                 &wasAliased);
  488.     if (error)
  489.         return badCount;
  490.         
  491.     if (targetIsFolder)
  492.     {
  493.         //    so, the FSSpecPtr described a folder
  494.         this->FileTypeRef() = kABFolderFileType;
  495.         //this->FileRefNumRef() = ::CurResFile();
  496.         count = 1;
  497.     } else {
  498.         //    well, the FSSpecPtr described a file, but
  499.         //    what kind? Let's try to find out initially
  500.         //    by examining the fileType code that the system
  501.         //    returns to us
  502.         //
  503.         
  504.         error = ::FSpGetFInfo (this->FSSpecPointerRef(), &fileInfo);
  505.         if (error == noErr)
  506.         {
  507.             //    check the file's type and decide then and there...save the
  508.             //    filetype into our member field "fileType" if it is acceptable
  509.             switch (fileInfo.fdType)
  510.             {
  511.                 case    kABPictResource:
  512.                 case    kABTextResource:
  513.                 case    kABTeachTextReadOnly:
  514.                         //    this is a good topic!
  515.                         this->FileTypeRef() = fileInfo.fdType;
  516.                         count = 1;
  517.                         break;
  518.                 default:
  519.                         //    well...it _could_ be a movie, but let someone
  520.                         //    else figure that one out! (Actually, that will
  521.                         //    be taken care of by our caller, so don't worry!)
  522.                         break;
  523.             } // end switch block
  524.         }
  525.         
  526.     } // end if block
  527.     
  528.     return count;
  529. }    //    end of function DoCheckSlides
  530.  
  531.  
  532.  
  533.  
  534. /*=============================== ABOtherTopic::Draw ================================*/
  535. OSErr    ABOtherTopic::Draw(WindowPtr window)
  536. {
  537.     OSErr        error = noErr;
  538.     
  539.     //    begin here...
  540.     //
  541.     short        oldResFile = this->UseTopicResFile();
  542.     
  543.     if (this->HasBlurb())
  544.     {
  545.         error = ABObject::Draw(window); 
  546.         if (!error)
  547.             error = this->BlurbRef()->Draw(window);
  548.         
  549.     } // end if else block
  550.     
  551.     ::UseResFile (oldResFile);
  552.     
  553.     return error;
  554.     
  555. } // end Draw
  556.  
  557.  
  558.  
  559.  
  560.  
  561. /*=============================== ABOtherTopic::Update ================================*/
  562. OSErr    ABOtherTopic::Update(WindowPtr window)
  563. {
  564.     OSErr        error = noErr;
  565.     
  566.     short        oldResFile = this->UseTopicResFile();
  567.     
  568.     //    begin here...
  569.     //
  570.     if (this->HasBlurb())
  571.     {
  572.         error = this->BlurbRef()->Update(window);
  573.     } else {
  574.         error = noErr;
  575.     } // end if else block
  576.     
  577.     ::UseResFile (oldResFile);
  578.     
  579.     return error;
  580. } // end Update
  581.  
  582.  
  583.  
  584.  
  585. /*=============================== ABOtherTopic::Event ================================*/
  586. Boolean    ABOtherTopic::Event(EventRecord *eventRec)
  587. {
  588.     Boolean    test = false;
  589.     
  590.     //    begin here...
  591.     //
  592.     short        oldResFile = this->UseTopicResFile();
  593.         
  594.     if (this->HasBlurb())
  595.         test = this->BlurbRef()->Event(eventRec);
  596.  
  597.     ::UseResFile (oldResFile);
  598.     
  599.     return test;
  600. } // end Event
  601.  
  602.  
  603.  
  604.  
  605.  
  606. /*=============================== ABOtherTopic::Stop ================================*/
  607. OSErr    ABOtherTopic::Stop(void)
  608. {
  609.     OSErr    error = noErr;
  610.     
  611.     short        oldResFile = this->UseTopicResFile();
  612.     
  613.     error = this->CloseTopic();
  614.     
  615.     ::UseResFile (oldResFile);
  616.     
  617.     return error;
  618.     
  619. } // end Stop
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626. //    end of file
  627.  
  628.